package com.sec.android.app.myfiles.module.optimizestorage;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.FolderSelectorBottomSheetActivity;
import com.sec.android.app.myfiles.dialog.ConfirmDeleteDialog;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.CopyMoveCmd;
import com.sec.android.app.myfiles.facade.cmd.DeleteRecordCmd;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.fragment.optimizestorage.OptimizeStorageFileListFragment;
import com.sec.android.app.myfiles.fragment.optimizestorage.OptimizeStorageFileListFragment.PauseCause;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationInfo.NavigationMode;
import com.sec.android.app.myfiles.operation.FileOperator;

import java.util.ArrayList;

/**
 * Created by daesu83.kim on 2016-10-07.
 */

public class OptimizeStorageMenuImp extends AbsMenuImp {

    public OptimizeStorageMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    protected void _setActionBarNormal(ActionBar actionbar) {
        if (actionbar != null) {
            actionbar.setCustomView(null);
            actionbar.setHomeButtonEnabled(true);
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setDisplayShowTitleEnabled(true);
            actionbar.setTitle(R.string.menu_get_more_space);
        }
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        int size = menu.size();
        MenuItem menuItem;
        for (int i = 0; i < size; i++) {
            menuItem = menu.getItem(i);
            if (menuItem != null) {
                menuItem.setVisible(false);
            }
        }
        menu.removeItem(R.id.menu_search);
    }

    @Override
    public void createContextualMenu(ContextMenu menu, AbsMyFilesFragment fragment, FileRecord record) {
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        return false;
    }

    @Override
    protected boolean _onOptionsItemSelected(MenuItem item) {
        boolean ret;
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_backup:
                ArrayList<FileRecord> selectedFiles = mFragment.getSelectedFile();
                String detail = SamsungAnalyticsLog.getCountOfExtensions(selectedFiles);
                long value = SamsungAnalyticsLog.getTotalSize(selectedFiles);
                SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.BACK_UP, value, detail, null);
                MyFilesFacade.copymoveRecord(mFragment.getProcessId(), mContext, FileOperator.Operation.BACKUP,
                        CopyMoveCmd.OperationProgress.SELECT_DST, mFragment, selectedFiles, null);
                if (mFragment instanceof OptimizeStorageFileListFragment) {
                    ((OptimizeStorageFileListFragment) mFragment).pauseReload(PauseCause.BACKUP);
                }
                ret = true;
                break;

            default:
                ret = super._onOptionsItemSelected(item);
        }
        return ret;
    }

    @Override
    protected boolean _onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case R.id.menu_delete:
                if (resultCode == Activity.RESULT_OK) {
                    if (mFragment instanceof OptimizeStorageFileListFragment) {
                        ((OptimizeStorageFileListFragment) mFragment).pauseReload(PauseCause.DELETE);
                    }
                } else if (resultCode == Activity.RESULT_CANCELED) {
                    if (data != null) {
                        int confirmCode = data.getIntExtra(ConfirmDeleteDialog.ARGS_CONFIRM_CODE, -1);
                        if (confirmCode != -1) {
                            DeleteRecordCmd.OperationProgress progress = DeleteRecordCmd.OperationProgress.values()[confirmCode];
                            if (progress == DeleteRecordCmd.OperationProgress.SHOW_OPTIMIZE_STORAGE_DELETE_CONFIRM_POPUP) {
                                mFragment.selectAll(false);
                            }
                        }
                    }
                    return true;
                }
                break;

            case FolderSelectorBottomSheetActivity.REQ_CODE_FOLDER_SELECTOR_BOTTOM_SHEET:
                // do not return any value. this requestCode should be handled in super call.
                if (mFragment instanceof OptimizeStorageFileListFragment) {
                    ((OptimizeStorageFileListFragment) mFragment).resumeReload(PauseCause.BACKUP);
                }
                break;
        }
        return super._onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void _onUpdateSelectLayoutMenu(Menu menu, NavigationMode navigationMode) {
        if (navigationMode == NavigationMode.Optimize_storage_files) {
            boolean selectedFileExist = mFragment.getSelectedFileCount() > 0;
            boolean supportBackupMenu = false;
            FileRecord curRecord = mNavigationInfo.getCurRecord();
            if (curRecord != null && curRecord.getStorageType() == StorageType.OptimizeStorage) {
                FileRecord.OptimizeStorageType optimizeStorageType = ((OptimizeStorageFileRecord) curRecord).getOptimizeStorageType();
                supportBackupMenu = optimizeStorageType == FileRecord.OptimizeStorageType.LargeFiles ||
                        optimizeStorageType == FileRecord.OptimizeStorageType.UnusedFiles;
            }
            setMenuItemVisibility(menu, R.id.menu_backup, supportBackupMenu && selectedFileExist);
            setMenuItemVisibility(menu, R.id.menu_delete, selectedFileExist);
        }
    }
}
